package com.ellricky.newtv.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.ellricky.newtv.R

class ImageAdapter : RecyclerView.Adapter<ImageAdapter.ViewHolder>() {

    private var imageList: ArrayList<String>? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_image, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        imageList?.also {
            if (it.isNotEmpty()) {
                val imageUrl = it[position]
                holder.bind(imageUrl)
            }
        }
    }

    override fun getItemCount(): Int {
        return imageList?.size ?: 0
    }


    class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        private val ivImage: ImageView = view.findViewById(R.id.iv_ppt)

        fun bind(imageUrl: String) {
            Glide.with(view.context).load(imageUrl).into(ivImage)
        }
    }

    fun setImages(images: ArrayList<String>) {
        this.imageList = images
        notifyDataSetChanged()
    }
}